package com.wtx.atguigu.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description:
 * @author: wtx
 * @createDate: 2020/6/8
 */
public class NioFileChannel02 {
    public static void main(String[] args) throws IOException {

        File file = new File("file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        // public abstract int read(ByteBuffer dst): dst->destination
        fileChannel.read(byteBuffer);

        // byteBuffer.array() 相当于获取 final byte[] hb
        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();
    }
}
